package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"test-paper/api/request"
	"test-paper/internal/dao"
	"test-paper/internal/model"
	"test-paper/pkg"
)

func CreateTest(c *gin.Context) {
	var req request.CreateTest
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var test model.Tests
	dao.GetOneByFields(&model.Tests{}, &test)
	if test.Title == req.Title {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "此套试卷一存在",
			"data": nil,
		})
		return
	}
	test = model.Tests{
		User:     req.User,
		Name:     req.Name,
		Title:    req.Title,
		Status:   req.Status,
		TestType: req.TestType,
	}
	if !dao.Create(&test) {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "试卷新增失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": test,
	})
}

func FindTest(c *gin.Context) {
	var test []model.Tests
	dao.GetListByFields(&model.Tests{}, &test)
	var tests []*model.Tests
	for _, t := range test {
		tests = append(tests, &t)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": tests,
	})
}

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		return
	}
	ctx := "E:\\GoWork\\专高四\\test-paper\\api\\微信图片_20250425170138.png" + file.Filename

	if file.Size > 3*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "文件过大",
			"data": nil,
		})
		return
	}

	//if file.Filename != "PDF" && file.Filename != "Word" {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": http.StatusBadRequest,
	//		"msg":  "文件格式不正确",
	//		"data": nil,
	//	})
	//	return
	//}

	upload, err := pkg.Upload(ctx)
	if err != nil {
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": upload,
	})
}
